﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace sieuthi
{
    public partial class khachhang : Form
    {
        private SqlDataAdapter da = new SqlDataAdapter();
        private SqlConnection conn = new SqlConnection(Threelayer.VO.core.connectionsring.ToString());
        private SqlCommand cmd = new SqlCommand();
        private DataTable dt = new DataTable("khachhang");
        Threelayer.VO.khachhangVO kh_vo;
        Threelayer.BUS.khachhangBUS kh_bus;
        public khachhang()
        {
            InitializeComponent();
        }

        private void khachhang_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'sieuthiDataSet.khachhang' table. You can move, or remove it, as needed.
            this.khachhangTableAdapter.Fill(this.sieuthiDataSet.khachhang);
            kh_binding();
            
        }
        private void kh_binding()
        {

            Box_DiaChi.DataBindings.Clear();
            Box_DiaChi.DataBindings.Add("text", dataGridkhachhang.DataSource, "diachi");
            box_Hoten.DataBindings.Clear();
            box_Hoten.DataBindings.Add("text", dataGridkhachhang.DataSource, "hoten");
            box_makh.DataBindings.Clear();
            box_makh.DataBindings.Add("text", dataGridkhachhang.DataSource, "maKH");
            box_khachhang_timkiem.DataBindings.Clear();
            box_khachhang_timkiem.DataBindings.Add("text", dataGridkhachhang.DataSource, "MaKh");
        }
        private void data_khachhang()
        {
            kh_vo = new Threelayer.VO.khachhangVO();
            kh_vo.DiaChi = Box_DiaChi.Text;
            kh_vo.Hoten = box_Hoten.Text;
            kh_vo.MaKH = box_makh.Text;
            
            
        }
        private void bt_khachhang_them_Click(object sender, EventArgs e)
        {
            data_khachhang();
            kh_bus = new Threelayer.BUS.khachhangBUS();
            kh_bus.khachhang_them(kh_vo);
            khachhang_Load(sender, e);
            
        }

        private void bt_khachhang_sua_Click(object sender, EventArgs e)
        {
            data_khachhang();
            kh_bus = new Threelayer.BUS.khachhangBUS();
            kh_bus.khachhang_sua(kh_vo);
            khachhang_Load(sender, e);
        }

        private void bt_khachhang_xoa_Click(object sender, EventArgs e)
        {
            data_khachhang();
            kh_bus = new Threelayer.BUS.khachhangBUS();
            kh_bus.khachhang_xoa(box_khachhang_timkiem.Text);
            khachhang_Load(sender, e);
        }

        private void bt_khachhang_timkiem_Click(object sender, EventArgs e)
        {
            dt.Clear();
            cmd.Dispose();
            cmd.Parameters.Clear();
            cmd.Connection = conn;
            cmd.CommandType = CommandType.StoredProcedure;
            conn.Open();
            cmd.CommandText = "khachhang_timkiem";
            cmd.Parameters.Add("@makh", SqlDbType.NVarChar, 200).Value = box_khachhang_timkiem.Text;
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            cmd.ExecuteNonQuery();
            da.Fill(dt);
            dataGridkhachhang.DataSource = dt;
            conn.Close();
            
        }
    }
}
